package com.kjsb.controller;

import com.kjsb.entity.ProjectCategory;
import com.kjsb.service.ProjectCategoryService;
import com.kjsb.service.ProjectManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@CrossOrigin
@RequestMapping("/projectCategorys")
public class ProjectCategoryController {
    @Autowired
    private ProjectCategoryService projectCategoryService;

    /**
     * 查询所有项目类信息
     *
     * @return Result结果集
     */
    @GetMapping
    public Result getAllProjectCategorys() {
        List<ProjectCategory> list = projectCategoryService.getAll();
        Integer code = list != null ? Code.GET_OK : Code.GET_ERR;
        String msg = list != null ? "查询成功！" : "数据查询失败，请重试！";
        return new Result(code, list, msg);
    }

    /**
     * 根据id查询项目类别
     *
     * @return Result结果集
     */
    @GetMapping("/{id}")
    public Result getProjectCategorys(@PathVariable("id") int id) {

        ProjectCategory one = projectCategoryService.getOneById(id);
        Integer code = one != null ? Code.GET_OK : Code.GET_ERR;
        String msg = one != null ? "查询成功！" : "数据查询失败，请重试！";
        return new Result(code, one, msg);

    }

    /**
     * 添加项目类别
     *
     * @return Result结果集
     */
    @PostMapping
    public Result saveProjectCategorys(@RequestBody ProjectCategory projectCategory) {
        Boolean flag = projectCategoryService.save(projectCategory);
        Integer code = flag ? Code.SAVE_OK : Code.SAVE_ERR;
        String msg = flag ? "添加成功" : "添加失败，请重试";
        return new Result(code, flag, msg);
    }

    /**
     * 修改项目类别
     *
     * @return Result结果集
     */
    @PutMapping()
    public Result updateProjectCategorys(@RequestBody ProjectCategory projectCategory) {
            Boolean flag = projectCategoryService.updateCategoryName(projectCategory);
            Integer code = flag ? Code.UPDATE_OK : Code.UPDATE_ERR;
            String msg = flag ? "修改成功" : "修改失败，请重试";
            return new Result(code, flag, msg);
    }
    /**
     * 根据id删除项目类别和其内的项目
     *
     * @return Result结果集
     */
    @DeleteMapping("/{id}")
    public Result deleteProjectByCategoryId(@PathVariable("id") int id) {
        List<Integer> list1 = projectCategoryService.getAllProjectByCategoryId(id);
        List<Integer> list2 = projectCategoryService.getAllUnitByCategoryId(id);
        if(list1.isEmpty()&&list2.isEmpty()){
            Boolean flag = projectCategoryService.removeById(id);
            Integer code = flag ? Code.DELETE_OK : Code.DELETE_ERR;
            String msg = flag ? "删除成功" : "删除失败，请重试";
            return new Result(code, flag, msg);
        }else {
            Boolean plan = projectCategoryService.removeAllProjectByCategoryId(id);
            Boolean plan2 = projectCategoryService.removeAllUnitByCategoryId(id);
            if (plan && plan2) {
                Boolean flag = projectCategoryService.removeById(id);
                Integer code = flag ? Code.DELETE_OK : Code.DELETE_ERR;
                String msg = flag ? "删除成功" : "删除失败，请重试";
                return new Result(code, flag, msg);
            } else {
                return new Result(Code.DELETE_EXCEPTION, false, "该项目类内项目可能被占用，无法全部删除，请稍后再试");
            }
        }
    }
    @GetMapping("/getExpertProject/{eid}")
    public Result getByEid(@PathVariable("eid") int eid) {
        List<ProjectCategory> one = projectCategoryService.getByEid(eid);
        Integer code = one != null ? Code.GET_OK : Code.GET_ERR;
        String msg = one != null ? "查询成功！" : "数据查询失败，请重试！";
        return new Result(code, one, msg);
    }
}
